package com.duguo.dynamicmvc.factory.utils;

import java.util.Map;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;

public class BeanFactoryUtils {
	public static <T>T getBeanOfType(BeanFactory beanFactory,Class<T> beanClass){
		Map beans=((ListableBeanFactory)beanFactory).getBeansOfType(beanClass,false,true);
		if(beans.size()==1){
			return (T)beans.values().iterator().next();
		}else if(beans.size()>1){
			throw new RuntimeException("There are more than one bean of type "+beanClass.getName()+" so you cannot get the bean by getBeanOfType()");
		}
		return null;
	}
}
